package org.jeecg.talentCooperation.util;

import org.springframework.util.ObjectUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description
 * @Author pkb
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2023/9/23
 */
public class ToolUtil {
    public static List<Integer> getRandomList(){
        List<Integer> result = new ArrayList<Integer>();
        int num = 3;
        int min = 1;
        int max = 7;
        int num1 =  getRandom(min, (max / num + 1));
        result.add(num1);
        int total = max;
        for (int i = 1; i < num; i++) {
            total = total - num1;
            while (total < min) {
                int maxc = Collections.max(result);
                result.set(result.indexOf(maxc), min);
                int s = result.stream().map(e -> e).reduce(Integer::sum).get();
                total = max - s;
            }
            if (i != num - 1) {
                num1 = getRandom(min, total);
                result.add(num1);
            } else {
                result.add(total);
            }
        }
        System.out.println("打印结果==="+result.toString());
        return result;
    }

    public static int getRandom(int MIN, int MAX) {
        Random random = new Random();
        return random.nextInt(MAX - MIN + 1) + MIN;
    }


    // 定义电话号码的正则表达式（匹配手机号码和固定电话）
    private static final String PHONE_PATTERN = "^(1[3-9]\\d{9})|(\\d{3,4}-\\d{7,8})$";

    // 判断电话号码是否正常
    public static boolean isNormalPhoneNumber(String phoneNumber) {
        return Pattern.matches(PHONE_PATTERN, phoneNumber) && phoneNumber.length() >= 8;
    }

    // 过滤非正常电话号码
    public static List<String> filterNormalPhoneNumbers(List<String> phoneNumbers) {
        List<String> normalPhoneNumbers = new ArrayList<>();
        for (String phoneNumber : phoneNumbers) {
            if (isNormalPhoneNumber(phoneNumber)) {
                normalPhoneNumbers.add(phoneNumber);
            }
        }
        return normalPhoneNumbers;
    }

    // 隐藏手机号
    public static String maskPhoneNumber(String phoneNumber) {
        // 使用正则表达式匹配所有可能的电话号码
        Pattern pattern = Pattern.compile(
                "(1[3-9]\\d{9})|(\\d{3}[-\\s]?\\d{4}[-\\s]?\\d{4})|(\\d{4}[-\\s]?\\d{3}[-\\s]?\\d{4})|(\\d{7,14})");
        Matcher matcher = pattern.matcher(phoneNumber);

        StringBuilder maskedPhones = new StringBuilder();

        while (matcher.find()) {
            String phone = matcher.group().replaceAll("[-\\s]", "");
            // 应用掩码
            if (phone.matches("1[3-9]\\d{9}")) { // 手机号码
                maskedPhones.append(phone.substring(0, 3)).append("****").append(phone.substring(7)).append(", ");
            } else if (phone.length() >= 7 && phone.length() <= 14) { // 座机号或区号+座机号
                // 对于不符合特定格式的数字序列，我们尝试识别区号和座机号，并相应地应用掩码
                if (phone.length() == 8 || phone.length() == 7) {
                    maskedPhones.append(phone.substring(0, 2)).append("****").append(phone.substring(6)).append(", ");
                } else if (phone.length() == 10 || phone.length() == 11) {
                    maskedPhones.append(phone.substring(0, 3)).append("****").append(phone.substring(7)).append(", ");
                } else {
                    maskedPhones.append(phone.substring(0, 3)).append("****").append(phone.substring(7)).append(", ");
                }
            } else {
                // 如果号码格式不符合预期，原样添加
                maskedPhones.append(phone).append(", ");
            }
        }

        // 移除最后一个逗号和空格
        if (maskedPhones.length() > 0) {
            maskedPhones.setLength(maskedPhones.length() - 2);
        }
        return maskedPhones.toString();
    }
    public static String imageToBase64(String imageUrl){
        if(ObjectUtils.isEmpty(imageUrl)){
            return "";
        }
        try {
            URL url = new URL(imageUrl);
            InputStream is = url.openStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            // 读取图片数据
            while ((length = is.read(buffer)) != -1) {
                baos.write(buffer, 0, length);
            }

            // 转换为Base64编码
            byte[] imageBytes = baos.toByteArray();
            String base64Image = Base64.getEncoder().encodeToString(imageBytes);

            return "data:image/png;base64," + base64Image; // 这里假设图片是PNG格式，根据实际情况调整MIME类型
        } catch (IOException e) {
            return "";
        }
    }

    // 测试方法
    public static void main(String[] args) {
//        List<String> phoneNumbers = Arrays.asList(
//                "3562", "08613839520", "12345678901", "abcdefghij", "123-456-7890", "18613839121",
//                "66603841", "18003218265", "not-a-phone", "13820672692", "18519004061", "13605706708",
//                "010-12345678", "020-87654321", "021-23456789", "01012345678");
//
//        List<String> normalPhoneNumbers = filterNormalPhoneNumbers(phoneNumbers);
//        System.out.println("正常电话号码列表：");
//        for (String number : normalPhoneNumbers) {
//            System.out.println(number);
//        }
        String text = "12345678912\n"
                + "63362634\n"
                + "78458285\n"
                + "010-58811524\n"
                + "66603843\n"
                + "66603491\n"
                + "63411751\n"
                + "63413386\n"
                + "63660759\n"
                + "66424775\n"
                + "52154784\n"
                + "66603843\n"
                + "66603496\n"
                // 省略了其他号码以节省空间
                + "12345678912";

        String maskedPhoneNumbers = maskPhoneNumber(text);
        System.out.println(maskedPhoneNumbers);

        System.out.println(ToolUtil.isNormalPhoneNumber("1"));
    }

}
